package com.battery.service.impl;

import com.battery.dao.Sys_UserDao;
import com.battery.pojo.Sys_User;
import com.battery.service.Sys_UserService;
import com.battery.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Sys_UserServiceImpl implements Sys_UserService {
    @Autowired
    private Sys_UserDao sys_UserDao;


    @Override
    public Result querySys_User(String sys_user_name){
        try{
            List<Sys_User> sys_User = sys_UserDao.selectSys_User(sys_user_name);
            if (sys_User == null) {
                return Result.error().message("查找无结果");
            }
            else {
                return Result.ok().data("sys_User", sys_User);
            }
        }catch (Exception e) {
            return Result.error().message("查找失败：" + e.toString());
        }
    }

    @Override
    public Result addSys_User(Sys_User sys_User){
        try{
            int flag = sys_UserDao.insertSys_User(sys_User);
            if (flag > 0){
                return Result.ok().message("增加成功");
            }
            else {
                return Result.error().message("增加失败");
            }
        }catch (Exception e) {
            return Result.error().message("增加失败：" + e.toString());
        }
    }

    @Override
    public Result dropSys_User(String sys_user_name){
        try{
            int flag = sys_UserDao.deleteSys_User(sys_user_name);
            if (flag > 0){
                return Result.ok().message("删除成功");
            }
            else {
                return Result.error().message("删除失败");
            }
        }catch (Exception e) {
            return Result.error().message("删除失败：" + e.toString());
        }
    }

    @Override
    public Result modifySys_User(Sys_User sys_User){
        try{
            int flag = sys_UserDao.updateSys_User(sys_User);
            if (flag > 0){
                return Result.ok().message("修改成功");
            }
            else {
                return Result.error().message("修改失败");
            }
        }catch (Exception e) {
            return Result.error().message("修改失败：" + e.toString());
        }
    }

    @Override
    public Result queryAllSys_User(){
        try{
            List<Sys_User> sys_User= sys_UserDao.getAllSys_User();
            if (sys_User == null){
                return Result.error().message("查找无结果");
            }
            else {
                return Result.ok().data("data",sys_User);
            }
        }catch (Exception e) {
            return Result.error().message("查找失败：" + e.toString());
        }
    }
}
